# -*- coding: utf-8 -*-
from scrapy import log
from twisted.enterprise import adbapi  # 导入twisted的包
import MySQLdb
import MySQLdb.cursors
import datetime


class IqiyiPipeline(object):
    def __init__(self):  # 初始化连接mysql的数据库相关信息
        self.dbpool = adbapi.ConnectionPool(
            dbapiName='MySQLdb',
            host='127.0.0.1',
            db='iqiyi',
            user='root',
            passwd='root',
            cursorclass=MySQLdb.cursors.DictCursor,
            charset='utf8',
            use_unicode=False
        )

    # pipeline dafault function                    #这个函数是pipeline默认调用的函数
    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self._conditional_insert, item)
        return item

    # insert the data to databases                 #把数据插入到数据库中
    def _conditional_insert(self, tx, item):
        lenname = len(item['name'])
        item['classification'] *= lenname
        for n in range(lenname):
            tx.execute("select * from tv1 where name=%s", (item['name'][n]))
            result = tx.fetchone()
            if result:
                pass
            else:
                sql = "insert into tv1 (name,classification,albumid,tvid)  values (%s,%s,%s,%s)"
                tx.execute(sql, (item['name'][n], item['classification'][n], item['albumid'][n], item['tvid'][n]))
